package com.note.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.note.common.ContextUtils;
import com.note.common.Result;
import com.note.domain.Content;
import com.note.domain.Note;
import com.note.domain.vo.ContentVo;
import com.note.service.ContentService;
import com.note.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/6 0:40
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"内容服务层"})
@Validated
@RequestMapping("note")
public class NoteController {

    @Autowired
    private NoteService noteService;

    @Autowired
    private ContentService contentService;

    @ApiModelProperty("内容分页")
    @GetMapping("page")
    public JSONObject notePageController(@Min(1) @RequestParam(value = "page",defaultValue = "1")
                                             @ApiParam("页码") Integer page,
                                         @Max(20)@RequestParam(value = "size",defaultValue = "10")
                                                 @ApiParam("条数")Integer size,
                                         @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        IPage<Note> page1=noteService.notePageListByKeyWord(page,size,keyword);
        return Result.successInfo("操作成功",page1);
    }

    @ApiModelProperty("时间搜索测试")
    @GetMapping("/test")
    public JSONObject noteDateSearchController()throws Exception{
        SimpleDateFormat dft=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date=dft.parse("2021/06/08 00:00:00");

        System.out.println(date);
        LambdaQueryWrapper<Note> noteLambdaQueryWrapper=new LambdaQueryWrapper<>();
        noteLambdaQueryWrapper.ge(Note::getCreateTime,date);

        List<Note> notes=noteService.list(noteLambdaQueryWrapper);
        return Result.successInfo("success",notes);

    }

    @ApiModelProperty("笔记新增")
    @PostMapping("save")
    public JSONObject saveNoteController(@RequestBody ContentVo content){
        Note note=new Note();
        note.setTitle(content.getTitle());
        note.setTag(content.getTag());
        note.setViewnum(0);
        //内容加密
        note.setIsEncrypt(true);
        noteService.save(note);

        Content content1=new Content();
        content1.setNid(note.getId());
        String contentStr= ContextUtils.encrpytHex(content.getContent());
        content1.setContent(contentStr);
        contentService.save(content1);
        return Result.successInfo("新增成功",note.getId());

    }

    /**
     * 插叙内容通过内容id
     * @return
     */
    @ApiModelProperty("内容查询")
    @GetMapping("get")
    public JSONObject contentByNidController(@RequestParam("nid")@ApiParam("内容id") Integer nid){
       Content content=contentService.getContentByNid(nid);
       if(content.getIsencrypt()){
          String contStr= ContextUtils.decryptStr(content.getContent());
          content.setContent(contStr);
       }
       noteService.addOneNoteReadCountByNid(nid);
       return Result.successInfo("success",content);
    }


    @ApiModelProperty("内容修改")
    @PostMapping("update")
    public JSONObject updateContentController(@NotNull@RequestBody ContentVo contentVo){
        if(StringUtils.isEmpty(contentVo.getId())){
            return Result.errorInfo("id不得为空");
        }
        Note note=noteService.getById(contentVo.getId());
        note.setTitle(contentVo.getTitle());
        note.setTag(contentVo.getTag());
        note.setIsEncrypt(true);
        noteService.updateById(note);

        LambdaQueryWrapper<Content> contentLambdaQueryWrapper=new LambdaQueryWrapper<>();
        contentLambdaQueryWrapper.eq(Content::getNid,note.getId());
        Content content=contentService.getOne(contentLambdaQueryWrapper,true);

        String contentStr=ContextUtils.encrpytHex(contentVo.getContent());
        content.setContent(contentStr);
        contentService.updateById(content);

        return Result.successInfo();

    }

    @ApiModelProperty("内容删除")
    @GetMapping("delete")
    public JSONObject deleteController(@RequestParam("id")Integer id){
        Note note=noteService.getById(id);
        if(note==null){
            return Result.successInfo("内容不存在");
        }
        noteService.removeById(id);
        return Result.successInfo("删除成功");
    }
}
